package io.shuttle.mbe.api

import io.shuttle.mbe.api.annotation.PromiseStyleMinVersion
import io.shuttle.mbe.api.types.Value1Function
import io.shuttle.mbe.api.types.VoidFunction
import io.shuttle.mbe.api.types.Window
import io.shuttle.mbe.core.Promise

////////////////////
// Sessions
////////////////////
/**
 * Use the `Browser.sessions` API to query and restore tabs and windows from a browsing session.
 *
 * Permissions: "sessions"
 */
interface Sessions {
    // The maximum number of sessions.Session that will be included in a requested list.default is 25
    val MAX_SESSION_RESULTS: Int

    // Retrieves all devices with synced sessions.
    // @return {callback.devices} The list of sessions.Device objects for each synced session, sorted in order from device with most recently modified session to device with least recently modified session. tabs.Tab objects are sorted by recency in the windows.Window of the sessions.Session objects.
    @PromiseStyleMinVersion(96)
     fun getDevices(
        filter: Filter?,
        callback: Value1Function<List<Device>>? = null
    ): Promise<List<Device>>

    // Gets the list of recently closed tabs and/or windows.
    // @return {callback.sessions} The list of closed entries in reverse order that they were closed (the most recently closed tab or window will be at index 0). The entries may contain either tabs or windows.
    @PromiseStyleMinVersion(96)
     fun getRecentlyClosed(
        filter: Filter?,
        callback: Value1Function<List<Session>>? = null
    ): Promise<List<Session>>

    // Reopens a windows.Window or tabs.Tab, with an optional callback to run when the entry has been restored.
    // @return {callback.restoredSession} A sessions.Session containing the restored windows.Window or tabs.Tab object.
    @PromiseStyleMinVersion(96)
     fun restore(
        // The windows.Window.sessionId, or tabs.Tab.sessionId to restore. If this parameter is not specified, the most recently closed session is restored.
        sessionId: String?,
        callback: Value1Function<Session>? = null
    ): Promise<Session>

    // Fired when recently closed tabs and/or windows are changed. This event does not monitor synced sessions changes.
     val onChanged: Events.Event<VoidFunction>

    data class Device(
        // The name of the foreign device.
        var deviceName: String,
        // A list of open window sessions for the foreign device, sorted from most recently to least recently modified session.
        var sessions: List<Session>,
    )

    data class Filter(
        // The maximum number of entries to be fetched in the requested list. Omit this parameter to fetch the maximum number of entries (sessions.MAX_SESSION_RESULTS).
        var maxResults: Number? = null,
    )

    data class Session(
        // The time when the window or tab was closed or modified, represented in seconds since the epoch.
        var lastModified: String,
        // The tabs.Tab, if this entry describes a tab. Either this or sessions.Session.window will be set.
        var tab: Tabs.Tab?,
        // The windows.Window, if this entry describes a window. Either this or sessions.Session.tab will be set.
        // FIXME: windows.Window
        var window: Window?,
    )
}